﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<List<nwrole.toolbox.sync.entity.SyncEntity>>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title>Index</title>
    <script src="/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
</head>
<body>
<a href="/entity/index">Entities</a> || <a href="/entity/schemaindex">Schema</a> || <a href="" onclick="return DeploySourceEnvironment();">DeploySource</a>|| <a href="" onclick="return DeployDestEnvironment();">DeployDestination</a><br />

    <div>
    <table>
    <tr>
        <th>Name</th>
        <th>Table</th>
        <th>Last sync</th>
        <th>Last import</th>
        <th>Deployed destination</th>
        <th>Sync type</th>
        <th>Enabled</th>
        <th>&nbsp;</th>
    </tr>

<% foreach (var e in Model)
   { %>        
        <tr>
            <td>
                <a href="/entity/show/<%=e.Id %>"><%=e.Name %></a>
            </td>        
            <td>
                <%=e.SourceTableName %>:
            </td>        
            <td>
                <%=e.SyncDat %>
            </td>        
            <td>
                <%=e.ImportDat %>
            </td>        
            <td>
                <%=e.DeployDestinationDat %>
            </td>        
            <td>
                <a href="" onclick="return ToggleSyncMode(this, <%=e.Id%>);"><%=e.SyncMode%></a>
            </td>
            <td>
                <a href="" onclick="return ToggleSyncEnabled(this, <%=e.Id%>);"><%=e.SyncEnabledString%></a>
            </td>    
            <td>
                 <a href="" onclick="return DeleteSyncEntity(this, <%=e.Id%>);">Delete</a>
                 <a href="" onclick="return DeploySource(this, <%=e.Id%>);">DeploySource</a>
                 <a href="" onclick="return DeployDestination(this, <%=e.Id%>);">DeployDestination</a>
                 <a href="" onclick="return SyncOnce(this, <%=e.Id%>);">Sync</a>
            </td>  
        </tr>
<% } %>    
    </table>    
    </div>
   
<script type="text/javascript">
    function ToggleSyncEnabled(obj, colId) {
        $.get('/entity/ToggleSyncEnabled', { id: colId, x: Math.random() }, function (result) {
            if (result.success) {
                if (result.newValue) {
                    $(obj).text('True');
                } else {
                    $(obj).text('False');
                }
            }
            else {
                alert(result.success);
            }
        });

        return false;
    }

    function ToggleSyncMode(obj, colId) {
        $.get('/entity/ToggleSyncMode', { id: colId, x: Math.random() }, function (result) {
            if (result.success) {
                $(obj).text(result.newValue);
            }
            else {
                alert(result.success);
            }
        });

        return false;
    }

    function ImportSchema() {
        $.get('/entity/ImportSchema', { a: 1, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function DeleteSyncEntity(obj, id) {
        $.get('/entity/DeleteSyncEntity', { id: id, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function DeploySource(obj, id) {
        $.get('/entity/DeploySource', { id: id, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function DeployDestination(obj, id) {
        $.get('/entity/DeployDestination', { id: id, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function SyncOnce(obj, id) {
        $.get('/entity/SyncOnce', { id: id, x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

    function DeploySourceEnvironment() {
        $.get('/entity/DeploySourceEnvironment', { x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }
    
    function DeployDestEnvironment() {
        $.get('/entity/DeployDestEnvironment', { x: Math.random() }, function (result) {
            if (result.success) {
                $('importSchemaLink').text('Done');
            }
            else {
                alert(result.success);
            }
        });
        return false;
    }

</script>

</body>
</html>
